<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="memDao" class="com.group.member.MemberDAO"/>
<%@page import="com.group.login.ZipcodeBean"%>
<%@page import="java.util.ArrayList"%>
<%
request.setCharacterEncoding("utf-8");
String check = request.getParameter("check");  // 검색 후 check에 n이 들어옴
String c_area3 = request.getParameter("area3");  //처음에 c_area3에 값이 없는 상태.

ArrayList<ZipcodeBean> list = memDao.zipcodeRead(c_area3); //값을 가지고 있음
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>우편번호 검색</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../js/script.js"></script>
<script type="text/javascript">
function dongCheck(){
	if(zipForm.area3.value === ""){
		alert("동이름을 입력하시오.");
		zipForm.area3.focus();
		return;
	}
	zipForm.submit();
}

function send(zipcode, area1, area2, area3, area4){
	var addr = area1 + " " + area2 + " " + area3 + " " + area4;
	//alert(zipcode + " " + addr);
	
	//검색값 원래 창으로 가져오고 검색창닫기.
	opener.document.regForm.zipcode.value=zipcode;
	opener.document.regForm.address.value=addr;
	window.close();
}
</script>
</head>
<body>
<center>
<b>** 우편번호 찾기 **</b>
<form action="zipCheck.jsp" name="zipForm" method="post">
<table>
	<tr>
		<td>
		동이름 입력: <input type="text" name="area3"/>
		<input type="button" value="검색" onclick="dongCheck()"/>
		<input type="hidden" name="check" value="n"/>
		<input type="button" value="닫기" onclick="javascript:window.close()"/>
		</td>
	</tr>
</table>
</form>

<%
if(check.equals("n")){ //검색완료. 자료가 있을경우 & 없을경우
	if(list.isEmpty()){
%>
		<b>검색된 자료가 없습니다 ㅠㅠㅠ</b>
<%
	}else{
%>
	<table>
		<tr>
			<td align="center">검색 자료를 클릭하면 자동으로 주소가 입력됩니다.</td>
		</tr>
		<tr>
			<td>
			<%
			for(int i=0; i<list.size(); i++){
				ZipcodeBean bean = (ZipcodeBean)list.get(i);
				String zipcode = bean.getZipcode();
				String area1 = bean.getArea1();
				String area2 = bean.getArea2();
				String area3 = bean.getArea3();
				String area4 = bean.getArea4();
				if(area4 == null) area4 = ""; //area4가 null인 경우 공백을 가지시오.
			%>
				<a href="javascript:send('<%=zipcode%>','<%=area1%>','<%=area2%>','<%=area3%>','<%=area4%>')">
				<%=area1%>&nbsp;<%=area2%>&nbsp;<%=area3%>&nbsp;<%=area4%>
				</a><br>
			<% 
			}
			%>
			</td>
		</tr>
	</table>	
<%	
	}
}
%>
</center>
</body>
</html>